Linear Search
Here is a description of linear search:
- Starting Conditions: You have an array of items and a target.
- Goal: Find the target in the array of items, or report if
the target is not present.
- Procedure: Look through the cells of the array one by one, starting
with the first cell and ending when either the target is found or
every cell has been examined.
- Result: Where the target was found, or a indication that it was not found.
- For this example: Return the index where the target was found, or -1 if the
target was not found.
Look over the array of Strings.
Apply the algorithm.
Is "Peoria" in the array?
Is "Albany" in the array?
Is "Mystic" in the array?
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Boston |
Albany |
Detroit |
Phoenix |
Peoria |
Albany |
Houston |
Hartford |